{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "machine_shape": "hm",
      "gpuType": "T4",
      "collapsed_sections": [
        "klzSfQPfwSNV"
      ],
      "authorship_tag": "ABX9TyPRDnVquohvsSsgBLv7amO+",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/dream80/roop_colab/blob/main/roop_v1_3.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "##一张图片，一个视频，一键换脸！\n",
        "\n",
        "[![打开](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/dream80/roop_colab/blob/main/roop_v1_3.ipynb)\n",
        "\n",
        "基于[roop](https://github.com/s0md3v/roop)项目的一个脚本你，可以进行图片换脸，也可以视频换脸。可以自定义参数。  \n",
        "\n",
        "特点：就是快捷方便，没有其他。  \n",
        "\n",
        "如果要高质量换脸还是推荐DeepFaclab[教程](https://www.deepfaker.xyz/)\n",
        "\n",
        "脚本仅供学习研究，请勿用于任何非法用途！\n",
        "\n",
        "使用教程：https://www.tonyisstark.com/1240.html  \n",
        "离线版本：https://www.tonyisstark.com/1311.html\n",
        "\n",
        "公众号：托尼不是塔克  \n",
        "博客：[tonyisstark.com](https://www.tonyisstark.com/)"
      ],
      "metadata": {
        "id": "glwNVbldZ1Id"
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "ZqdQ95WTKCSQ",
        "cellView": "form"
      },
      "outputs": [],
      "source": [
        "#@title 1.克隆源代码\n",
        "!git clone https://github.com/s0md3v/roop.git\n",
        "!git clone https://github.com/dream80/roop_colab.git\n"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "#@title 2.进入项目\n",
        "%cd /content/roop\n",
        "\n",
        "Version = \"v1.3.2\" #@param [\"v1.3.0\",\"v1.3.1\",\"v1.3.2\"]\n",
        "\n",
        "vdict = {\n",
        "    \"v1.3.0\": \"142284916a5448acdf1a7fdca56d01c5aecafdad\",\n",
        "    \"v1.3.1\": \"e0642702f388ea16089a1d4a929728fa69457d60\",\n",
        "    \"v1.3.2\": \"c2d1feb17a9c51061b52cae5897136528f3b80cc\",\n",
        "}\n",
        "\n",
        "commit=vdict.get(Version, \"Invalid Version\")\n",
        "\n",
        "vcmd= f\"checkout {commit}\"\n",
        "\n",
        "\n",
        "!git $vcmd"
      ],
      "metadata": {
        "id": "FnSwYDX5K1P2",
        "cellView": "form"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "#@title 3.安装依赖\n",
        "#有红色警告不用担心！\n",
        "%cd /content/roop\n",
        "!pip install -r requirements.txt\n",
        "!pip install onnxruntime-gpu==1.17.0 --index-url=https://pkgs.dev.azure.com/onnxruntime/onnxruntime/_packaging/onnxruntime-cuda-12/pypi/simple\n",
        "#!pip install onnxruntime-gpu==1.15.0"
      ],
      "metadata": {
        "id": "f6urrvLGKqcU",
        "cellView": "form"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "#@title 4.准备素材\n",
        "source = \"/content/roop_colab/1.jpg\" #@param {type:\"string\"}\n",
        "target = \"/content/roop_colab/2.mp4\" #@param {type:\"string\"}\n",
        "output = \"/content/roop_colab/out.mp4\" #@param {type:\"string\"}"
      ],
      "metadata": {
        "cellView": "form",
        "id": "JXUkjpNpz2pc"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "source : 源素素材路径，包含人脸的图片  \n",
        "\n",
        "target : 目标图片或者视频路径，也要包含人脸  \n",
        "\n",
        "output : 输出路径，如果targe是图片，这里也改成图片后缀  \n"
      ],
      "metadata": {
        "id": "vW_Ovjjq8Qv8"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "#@title 5.开始换脸\n",
        "\n",
        "%cd /content/roop\n",
        "\n",
        "Device = \"cuda\" #@param [\"cuda\", \"cpu\"]\n",
        "\n",
        "Processor = \"face_swapper face_enhancer\" #@param [\"face_swapper face_enhancer\", \"face_swapper\",\"face_enhancer\"]\n",
        "\n",
        "VideoEncoder = \"libx264\" #@param ['libx264', 'libx265', 'libvpx-vp9', 'h264_nvenc', 'hevc_nvenc']\n",
        "\n",
        "VideoQuality = \"35\" #@param {type:\"string\"}\n",
        "\n",
        "TempFormat = \"jpg\" #@param [\"jpg\", \"png\"]\n",
        "\n",
        "TempQuality = \"0\" #@param {type:\"string\"}\n",
        "\n",
        "\n",
        "KeepFPS = True #@param {type:\"boolean\"}\n",
        "SkipAudio = False #@param {type:\"boolean\"}\n",
        "KeepFrames = False #@param {type:\"boolean\"}\n",
        "ManyFaces = False #@param {type:\"boolean\"}\n",
        "\n",
        "\n",
        "KeepFPS =\"--keep-fps\" if KeepFPS==True else \"\"\n",
        "SkipAudio =\"--skip-audio\" if SkipAudio==True else \"\"\n",
        "KeepFrames =\"--keep-frames\" if KeepFrames==True else \"\"\n",
        "ManyFaces =\"--many-faces\" if ManyFaces==True else \"\"\n",
        "\n",
        "\n",
        "\n",
        "cmd = f\"run.py --execution-provider {Device} -s {source} -t {target} -o {output} --frame-processor {Processor} --output-video-encoder {VideoEncoder} --output-video-quality {VideoQuality} {KeepFPS} {SkipAudio} {KeepFrames} {ManyFaces} --temp-frame-format {TempFormat} --temp-frame-quality {TempQuality}\"\n",
        "print(\"cmd:\"+cmd)\n",
        "\n",
        "\n",
        "!wget https://github.com/dream80/roop_colab/releases/download/v0.0.1/inswapper_128.onnx\n",
        "!mkdir models\n",
        "!mv inswapper_128.onnx models/\n",
        "\n",
        "!wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.4/GFPGANv1.4.pth\n",
        "!mv GFPGANv1.4.pth models/\n",
        "\n",
        "!python $cmd\n"
      ],
      "metadata": {
        "id": "cMJi9bI3LUyX",
        "cellView": "form"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Device : 选CUDA就是使用GPU，选CPU就用CPU，如果你GPU配额用完了，可以用CPU顶一下。或者开Pro  \n",
        "\n",
        "Precessor : 选择换脸还是人脸修复，默认全部启用，也可以单独使用一个功能。  \n",
        "\n",
        "VideoEncoder : 视频编码器选择，一般用libx264。  \n",
        "\n",
        "VideoQuality : 视频质量，默认35，取值范围0-100  \n",
        "\n",
        "TempFormat ： 缓存帧格式，可以使jpg或者png  \n",
        "\n",
        "TempQuality ： 视频质量，默认0，取值范围0-100  \n",
        "\n",
        "KeepFPS : 保持帧率不变  \n",
        "\n",
        "SkipAudio ： 丢弃音轨   \n",
        "\n",
        "KeepFrames ： 保留分帧后的图片  \n",
        "\n",
        "ManyFaces ： 替换所有人脸"
      ],
      "metadata": {
        "id": "iOOzIywkxw9o"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "#补充说明\n",
        "\n",
        "  \n"
      ],
      "metadata": {
        "id": "P_cIVG84cTX3"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "\n",
        "## 视频文件在哪里？  \n",
        "\n",
        "换脸成功后的视频保存在roop_colab文件里面  \n"
      ],
      "metadata": {
        "id": "O8qkUHNNw8-z"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "## 如何使用自己的素材？  \n",
        "\n",
        "如果要替换图片和视频，只需要在左边的文件管理里面右键上传，然后改一下名字就可以。了。  \n",
        "素材文件都在roop_colab文件中   \n",
        "1.jpg：换脸图片  \n",
        "2.mp4：换脸视频  \n",
        "out.mp4：是换脸后的视频  \n",
        "\n",
        "点击左侧文件，找到MP4,右键可以下载！\n",
        "\n"
      ],
      "metadata": {
        "id": "pl3m7Ymmw3A8"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "## 解除NSFW封印\n",
        "\n",
        "不多说，提示都在这里了  \n",
        "MAX_PROBABILITY = 1\n"
      ],
      "metadata": {
        "id": "kUXiEHjCxMpt"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "\n",
        "## 大文件处理\n",
        "\n",
        "如果文件比较大，直接在Colab上传会有难度，可以先传到谷歌云盘。然后挂载云盘就可以了，修改一下第五步里面图片和视频的地址就可以了。挂载谷歌盘之后也可以把输出文件直接指向云盘。\n",
        "\n",
        "如何需要使用谷歌云盘的文件，可以点击这个脚本，挂载云盘。\n",
        "\n",
        "from google.colab import drive  \n",
        "drive.mount('/content/drive')"
      ],
      "metadata": {
        "id": "klzSfQPfwSNV"
      }
    }
  ]
}
